package com.redpacketcover.rc.goods;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.redpacketcover.rc.Constants;
import com.redpacketcover.rc.R;
import com.redpacketcover.rc.base.BasicActivity;
import com.redpacketcover.rc.bean.GoodsRecord;
import com.redpacketcover.rc.carts.CartsActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class GoodsDetailActivity extends BasicActivity<GoodsPresenter> implements IMatchView, OnBannerListener {

    private int id;
    private int formatId;

    private Banner banner;
    private ImageView iv_return;
    private View layout_paddingTop;
    private TextView goods_price;
    private TextView goods_oldprice;
    private LinearLayout goods_tag_layout;
    private LinearLayout norm_list_layout;
    private TextView goods_title;
    private WebView goods_detail;
    private TextView goods_remark;
    private TextView service;//客服
    private TextView cart;//购物车
    private TextView add_cart_btn;//加入购物车
    private TextView buy_now;//立即购买

    private ArrayList<String> list_path = new ArrayList<>();
    private ArrayList<String> list_title = new ArrayList<>();

    private List<String> taglist = new ArrayList<>();
    private List<GoodsRecord.NormRecord> normlist = new ArrayList<>();

    @Override
    protected int initLayout() {
        return R.layout.goods_detail_layout;
    }

    @Override
    protected GoodsPresenter createPresenter() {
        return new GoodsPresenter();
    }

    @Override
    protected void initViews() {
        setHideStatus();
        banner = findViewById(R.id.home_banner);
        iv_return = findViewById(R.id.return_back);
        goods_price = findViewById(R.id.goods_price);
        goods_oldprice = findViewById(R.id.goods_oldprice);
        goods_tag_layout = findViewById(R.id.goods_tag_layout);
        goods_title = findViewById(R.id.goods_title);
        norm_list_layout = findViewById(R.id.norm_list_layout);
        goods_remark = findViewById(R.id.goods_remark);
        goods_detail = findViewById(R.id.goods_detail);
        service = findViewById(R.id.service);
        cart = findViewById(R.id.cart);
        add_cart_btn = findViewById(R.id.add_cart_btn);
        buy_now = findViewById(R.id.buy_now);

        layout_paddingTop = findViewById(R.id.layout_paddingTop);
        int tops = getStatusBarHeight(this);
        layout_paddingTop.setPadding(0,tops,0,0);

        banner.setBannerStyle(BannerConfig.NOT_INDICATOR);
        banner.setImageLoader(new MyLoader());
        banner.setDelayTime(3000);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);

        mPresenter.getGoodsDetail(id);
        iv_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),CartsActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    public void showGoodsDetailResult(GoodsRecord data) {
        formatId = data.getNormList().get(0).getId();

        for(int i=0;i<data.getImageLibList().size();i++){
            list_path.add(Constants.URL_HOST_TEST+data.getImageLibList().get(i).getPic());
            list_title.add("banner"+i);
        }

        banner.setImages(list_path);
        banner.setBannerTitles(list_title);
        banner.start();

        goods_price.setText("¥"+data.getPrice());
        goods_oldprice.setText("¥"+data.getOldPrice());
        goods_oldprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

        taglist = new ArrayList<>();
        if(data.getTagArr()!=null){
            for(int i=0 ;i<data.getTagArr().size();i++){
                taglist.add(data.getTagArr().get(i).getTitle());
                View view = initTagView();
                if(view != null){
                    goods_tag_layout.addView(view);
                }
            }
        }

        goods_title.setText(data.getTitle());


        for(int i=0 ;i<data.getNormList().size();i++){
            normlist.add(data.getNormList().get(i));
            View view = initNormView(i);
            norm_list_layout.addView(view);
        }

        goods_remark.setText(data.getBrief());

        String str = data.getDetail().replace("<img","<img style='max-width:100%'");
        goods_detail.loadDataWithBaseURL(null, str, "text/html", "UTF-8", null);
    }

    @Override
    public void showcartsListResult(GoodsRecord data) {

    }

    private View initNormView(int j){
        View view = View.inflate(this, R.layout.tag_norm_layout, null);
        for(int i = 0;i<normlist.size();i++){
            TextView tv = (TextView) view.findViewById(R.id.tag_norm_title);
            tv.setText(normlist.get(i).getTitle()+"￥"+normlist.get(i).getPrice());
            if(j == 0){
                tv.setEnabled(true);
            }
        }
        return view;
    }

    private View initTagView() {
        if(taglist.size() == 0){
            return null;
        }else{
            View view = View.inflate(this, R.layout.tag_layout, null);
            for(int i = 0;i<taglist.size();i++){
                TextView tv = (TextView) view.findViewById(R.id.tag_title);
                tv.setText(taglist.get(i));
            }
            return view;
        }
    }

    @Override
    public void OnBannerClick(int position) {
        Log.i("tag", "你点了第"+position+"张轮播图");
    }

    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load((String) path).into(imageView);
        }
    }

    @Override
    public void showGoodsListResult(List<GoodsRecord> data) {

    }
}
